package com.zheng.umbrella.web.controller.manage;

import com.zheng.common.base.BaseController;
import com.zheng.common.constant.Constant;
import com.zheng.common.util.JsonUtil;
import com.zheng.common.util.PayCommonUtil;
import com.zheng.common.util.QRCodeUtil;
import com.zheng.common.util.ZipUtil;
import com.zheng.umbrella.common.constant.UmbrellaConstant;
import com.zheng.umbrella.common.constant.UmbrellaResult;
import com.zheng.umbrella.common.constant.UmbrellaResultConstant;
import com.zheng.umbrella.dao.model.*;
import com.zheng.umbrella.rpc.api.*;
import com.zheng.upms.dao.model.UpmsAgent;
import com.zheng.upms.dao.model.UpmsAgentExample;
import com.zheng.upms.dao.model.UpmsDict;
import com.zheng.upms.rpc.api.UpmsAgentService;
import com.zheng.upms.rpc.api.UpmsApiService;
import com.zheng.upms.rpc.api.UpmsDictService;
import com.zheng.upms.rpc.api.UpmsSystemParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
import java.util.zip.ZipOutputStream;


/**
 * Created by Administrator on 2018/9/16.
 */
@Controller
@Api(value = "OTA软件版本管理", description = "OTA软件版本管理")
@RequestMapping("/manage/version")
public class UmbrellaHardwareVersionController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UmbrellaHardwareVersionController.class);

    @Autowired
    private UmbrellaHardwareMachineVersionService umbrellaHardwareMachineVersionService;

    @Autowired
    private UmbrellaHardwareMachineVersionDetailsService umbrellaHardwareMachineVersionDetailsService;

    @Autowired
    private UpmsApiService upmsApiService;

    @Autowired
    UpmsSystemParamService upmsSystemParamService;

    @Autowired
    UpmsDictService upmsDictService;


    @ApiOperation(value = "版本首页")
    @RequiresPermissions("umbrella:version:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/version/index.jsp";
    }

    @ApiOperation(value = "版本列表")
    @RequiresPermissions("umbrella:version:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order,
            @RequestParam(required = false, value = "machineCode") String machineCode,
            @RequestParam(required = false, value = "status") Integer status) {
        String userName = getLoginUserName();
        Integer agentId = upmsApiService.selectUserAgentIdByUsername(userName);
        UmbrellaHardwareMachineVersionExample umbrellaHardwareMachineVersionExample = new UmbrellaHardwareMachineVersionExample();
        UmbrellaHardwareMachineVersionExample.Criteria criteria = umbrellaHardwareMachineVersionExample.createCriteria();

        List<UmbrellaHardwareMachineVersion> rows = umbrellaHardwareMachineVersionService.selectByExampleForOffsetPage(umbrellaHardwareMachineVersionExample, offset, limit);
        long total = umbrellaHardwareMachineVersionService.countByExample(umbrellaHardwareMachineVersionExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增版本")
    @RequiresPermissions("umbrella:version:create")
    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String create(ModelMap modelMap) {
        //版本状态
        List<UpmsDict> statusList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_UMBRELLA_MACHINE_STATUS);
        modelMap.put("statusList", statusList);

        String loginUserName = getLoginUserName();
        Integer agentId = upmsApiService.selectUserAgentIdByUsername(loginUserName);
        long nowtime = new Date().getTime();

        //版本型号
        List<UpmsDict> machineTypeList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_MACHINE_TYPE);
        modelMap.put("machineTypeList", machineTypeList);
        return "/manage/version/create.jsp";
    }

    @ApiOperation(value = "新增版本")
    @RequiresPermissions("umbrella:version:create")
    @ResponseBody
    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    public Object create(UmbrellaHardwareMachineVersion UmbrellaHardwareMachineVersion) {
//        ComplexResult result = FluentValidator.checkAll()
//                .on(upmsAgent.getName(), new LengthValidator(1, 120, "名称"))
//                .doValidate()
//                .result(ResultCollectors.toComplex());
//        if (!result.isSuccess()) {
//            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
//        }
        String userName = getLoginUserName();
        Integer agentId = upmsApiService.selectUserAgentIdByUsername(userName);
        long time = System.currentTimeMillis();
        int count = umbrellaHardwareMachineVersionService.insertSelective(UmbrellaHardwareMachineVersion);
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除版本")
    @RequiresPermissions("umbrella:version:delete")
    @RequestMapping(value = "/delete/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {

        String[] idArray = ids.split("-");
        ArrayList<Integer> idList = new ArrayList<Integer>();
        for(int i=0;i<idArray.length;i++){
            idList.add(Integer.parseInt(idArray[i]));
        }
        UmbrellaHardwareMachineVersionExample machineExample = new UmbrellaHardwareMachineVersionExample();
        machineExample.createCriteria().andIdIn(idList);
        List<UmbrellaHardwareMachineVersion> machineList = umbrellaHardwareMachineVersionService.selectByExample(machineExample);

//        int count = UmbrellaHardwareMachineVersionService.deleteByPrimaryKeys(ids);
        int count = umbrellaHardwareMachineVersionService.deleteLogicByPrimaryKeys(ids,UmbrellaHardwareMachineVersion.class,null);

        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改版本")
    @RequiresPermissions("umbrella:version:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.GET})
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        //版本状态
        List<UpmsDict> statusList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_UMBRELLA_MACHINE_STATUS);
        modelMap.put("statusList", statusList);
        String loginUserName = getLoginUserName();
        int agentId = upmsApiService.selectUserAgentIdByUsername(loginUserName);

        UmbrellaHardwareMachineVersion UmbrellaHardwareMachineVersion = umbrellaHardwareMachineVersionService.selectByPrimaryKey(id);
        modelMap.put("UmbrellaHardwareMachineVersion", UmbrellaHardwareMachineVersion);
        return "/manage/version/update.jsp";
    }

    @ApiOperation(value = "修改版本")
    @RequiresPermissions("umbrella:version:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public Object update(@PathVariable("id") Integer id, UmbrellaHardwareMachineVersion UmbrellaHardwareMachineVersion) {
        UmbrellaHardwareMachineVersion.setId(id);
        int count = umbrellaHardwareMachineVersionService.updateByPrimaryKeySelective(UmbrellaHardwareMachineVersion);
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }
}
